import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Vector;

/**
 * @author lys
 * 管理图书
 */
public class ManageBook {
    private static ManageBook instance;

    public JPanel jPanel = new JPanel();

    public JLayeredPane jLayeredPane = new JLayeredPane();

    private JLabel jLabel = new JLabel("图书管理");
    private JLabel jLabel2 = new JLabel("请选择查询方式：");

    private JTextField field = new JTextField(25);

    private Dimension dimension = new Dimension(220, 30);

    private JComboBox<String> box = new JComboBox<String>();

    private JButton button = new JButton("搜索");
    private JButton button2 = new JButton("增加新书");
    private JButton button3 = new JButton("更新书籍");
    private JButton button4 = new JButton("删除书籍");
    private JButton button5 = new JButton("增加类别");
    private JButton button6 = new JButton("更新类别");
    private JButton button7 = new JButton("删除类别");

    public DefaultTableModel model = new DefaultTableModel();

    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font1 = new Font("宋体", Font.BOLD, 25);
    private Font font2 = new Font("宋体", Font.BOLD, 20);

    JTable jTable;

    private String s;
    private String book;
    private int id;

    private ManageBook() {
        Icon i = new ImageIcon(ManageBook.class.getResource("img/4.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);

        jLabel.setFont(font);
        jLabel.setBounds(485, 35, 800, 100);
        jLabel.setForeground(Color.black);

        jLabel2.setFont(font1);
        jLabel2.setBounds(180, 130, 250, 30);
        jLabel2.setForeground(Color.black);

        box.setSize(dimension);
        box.addItem("按照类别查找");
        box.addItem("按照书名查找");
        box.addItem("按照作者查找");
        box.addItem("按照书号查找");
        box.setFont(font2);
        box.setBounds(180, 170, 200, 40);
        box.setOpaque(false);

        // 文本框
        field.setFont(font2);
        field.setSize(dimension);
        field.setBackground(Color.cyan);
        field.setBounds(480, 173, 250, 35);
        field.setForeground(Color.white);
        field.setOpaque(false);

        // 按钮
        button.setFont(font1);
        button.setBounds(850, 170, 100, 40);
        button.setForeground(Color.black);
        button.setBackground(Color.white);
        button.setOpaque(false);

        button2.setFont(font1);
        button2.setBounds(150,250,150,50);
        button2.setForeground(Color.black);
        button2.setBackground(Color.white);
        button2.setOpaque(false);

        button3.setFont(font1);
        button3.setBounds(150,320,150,50);
        button3.setForeground(Color.black);
        button3.setBackground(Color.white);
        button3.setOpaque(false);

        button4.setFont(font1);
        button4.setBounds(150,380,150,50);
        button4.setForeground(Color.black);
        button4.setBackground(Color.white);
        button4.setOpaque(false);

        button5.setFont(font1);
        button5.setBounds(150,440,150,50);
        button5.setForeground(Color.black);
        button5.setBackground(Color.white);
        button5.setOpaque(false);

        button6.setFont(font1);
        button6.setBounds(150,500,150,50);
        button6.setForeground(Color.black);
        button6.setBackground(Color.white);
        button6.setOpaque(false);

        button7.setFont(font1);
        button7.setBounds(150,560,150,50);
        button7.setForeground(Color.black);
        button7.setBackground(Color.white);
        button7.setOpaque(false);

        // 表格
        model.addColumn("书号", new Vector<Integer>());
        model.addColumn("类别", new Vector<Integer>());
        model.addColumn("书名", new Vector<Integer>());
        model.addColumn("作者", new Vector<Integer>());
        model.addColumn("出版社", new Vector<Integer>());
        model.addColumn("状态", new Vector<Integer>());
        jTable = new JTable(model);
        jTable.setOpaque(false);

        JScrollPane pane = new JScrollPane(jTable);
        pane.setBounds(380, 250, 700, 400);
        pane.setOpaque(false);

        jTable.setVisible(false);
        pane.setVisible(false);

//		for(int k = 0; k < 30; k++) {
//			model.addRow(new Vector<Integer>());
//		}
        // FindBook.allbook(model);

        JTableHeader head = jTable.getTableHeader();
        // 设置表头的大小
        head.setPreferredSize(new Dimension(head.getWidth(), 30));
        // 设置表头字体大小
        head.setFont(new Font("宋体", Font.BOLD, 20));
        // head.setForeground(Color.cyan);
        head.setBackground(Color.cyan);
        // 设置表格的行宽
        jTable.setRowHeight(30);
        // 设置表格行中字体大小
        jTable.setFont(new Font("宋体", Font.ROMAN_BASELINE, 17));

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        jTable.setDefaultRenderer(Object.class, renderer);

        jLayeredPane.add(jLabel, new Integer(100), 1);
        jLayeredPane.add(jLabel2, new Integer(100), 2);
        jLayeredPane.add(box, new Integer(100), 3);
        jLayeredPane.add(field, new Integer(100), 4);
        jLayeredPane.add(button, new Integer(100), 5);
        jLayeredPane.add(button2,100,6);
        jLayeredPane.add(button3,100,7);
        jLayeredPane.add(button4,100,8);
        jLayeredPane.add(button5,100,9);
        jLayeredPane.add(button6,100,10);
        jLayeredPane.add(button7,100,11);
        jLayeredPane.add(pane, new Integer(100), 12);
        jLayeredPane.add(Label, new Integer(0), 0);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTable.setVisible(true);
                pane.setVisible(true);
                String str = (String)box.getSelectedItem();
                switch (str){
                    case "按照类别查找":
                        type = 1;
                        break;
                    case "按照书名查找":
                        type = 2;
                        break;
                    case "按照作者查找":
                        type = 3;
                        break;
                    case "按照书号查找":
                        type = 4;
                        break;
                }
                ArrayList<Book> books = Search.returnSearch(type, field.getText());
                if(books != null){
                    DefaultTableModel model2 = (DefaultTableModel) jTable.getModel();
                    model2.getDataVector().clear();
                    model2.fireTableDataChanged();
                    jTable.updateUI();
                    for(Book book: books){
                        String[] column = {String.valueOf(book.getBookId()), book.getType(), book.getBookName(), book.getWriter(), book.getPress(), "" };
                        if(book.isBorrowed()){
                            column[5] = "外借";
                        }
                        else{
                            column[5] = "在馆";
                        }
                        model2.addRow(column);
                    }
                }
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminInterface.getInstance().admin.setVisible(false);
                AddBook.getInstance().setVisible(true);
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminInterface.getInstance().admin.setVisible(false);
                UpdateBook.getInstance().setVisible(true);
            }
        });

        button4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminInterface.getInstance().admin.setVisible(false);
                DeleteBook.getInstance().setVisible(true);
            }
        });

        button5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminInterface.getInstance().admin.setVisible(false);
                AddType.getInstance().setVisible(true);
                AddType.getInstance();
            }
        });

        button6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminInterface.getInstance().admin.setVisible(false);
                UpdateType.getInstance().setVisible(true);
                UpdateType.getInstance();
            }
        });

        button7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AdminInterface.getInstance().admin.setVisible(false);
                DeleteType.getInstance().setVisible(true);
                DeleteType.getInstance();
            }
        });

        button.setFocusPainted(false);
        button2.setFocusPainted(false);
        button3.setFocusPainted(false);
        button4.setFocusPainted(false);
        button5.setFocusPainted(false);
        button6.setFocusPainted(false);
        button7.setFocusPainted(false);
    }

    public static ManageBook getInstance() {
        if (instance == null) instance = new ManageBook();
        return instance;
    }
    /**
     * @author zj
     */
    private int type = 1;
    public static void removeInstance(){
        instance = null;
    }

    /**
     * 展示所有书籍信息
     */
    public void showBooks(){
        ArrayList<Book> books = Search.returnSearch(1, "");
        if(books != null){
            DefaultTableModel model2 = (DefaultTableModel) jTable.getModel();
            model2.getDataVector().clear();
            model2.fireTableDataChanged();
            jTable.updateUI();
            for(Book book: books){
                String[] column = {String.valueOf(book.getBookId()), book.getType(), book.getBookName(), book.getWriter(), book.getPress(), "" };
                if(book.isBorrowed()){
                    column[5] = "外借";
                }
                else{
                    column[5] = "在馆";
                }
                model2.addRow(column);
            }
        }
    }
}
